//
//  HMWelcomViewController.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HMWelcomViewController: UIViewController {
    override func loadView() {
        view = backView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    override func viewDidAppear(animated: Bool) {
        startAnimation()
    }
    func startAnimation(){
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: .CurveLinear, animations: {
            self.iconView.snp_updateConstraints { (make) in
                make.bottom.equalTo(self.view).offset(-ScreenHeigh+160)
            }
            self.view.layoutIfNeeded()
            }) { (_) in
            UIView.animateWithDuration(0.5, animations: { 
                self.nameLabel.alpha = 1
                }, completion: { (_) in
                //切换根控制器
//                UIApplication.sharedApplication().keyWindow?.rootViewController = HMMainTabBarController()
            NSNotificationCenter.defaultCenter().postNotificationName(switchRootViewController, object: "main")
            })
                
        }
    }
    func setupUI() -> Void {
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) in
            make.bottom.equalTo(view).offset(-160)
            make.centerX.equalTo(view)
            make.width.height.equalTo(90)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.centerX.equalTo(view)
        }
        nameLabel.alpha = 0
        
        let iconUrlStr = HMUserAccountViewModel.shareAccountViewModel.userAccount?.avatar_large ?? ""
        iconView.sd_setImageWithURL(NSURL(string: iconUrlStr))
        iconView.layer.cornerRadius = 45
        
        
    }
    
    private lazy var backView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel = { ()->(UILabel) in
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(17)
        l.textColor = UIColor.darkGrayColor()
        l.text = HMUserAccountViewModel.shareAccountViewModel.userAccount?.name
        l.textAlignment = .Center
        l.sizeToFit()
        return l
    }()
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}
